package inabit.inabitcompiler.parser;

import inabit.compiler.parser.Parser;
import inabit.data.InabitMethod;
import inabit.data.Rule;
import inabit.data.Variable;
import inabit.inabitcompiler.lexical.InabitLexicalAnalyzer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**Represents the syntactic and semantic analyzer, it analyzes the file
 * read by the lexical analyzer while execute the semantic actions when necessary*/
/**@author Rafael Rocha*/
public class InabitParser extends Parser<InabitSemanticAction>{
	
	private Map<String, InabitMethod> methods;
	private List<Rule> rules;
	private List<Variable> variables;
	
	/**Receives the lexical analyzer and the context-free grammar of the language*/
	public InabitParser() {
		super(new InabitLexicalAnalyzer(), InabitGrammar.buildGrammar());
		rules = new ArrayList<Rule>();
		methods = new HashMap<String, InabitMethod>();
		variables = new ArrayList<Variable>();
		setPrintCompilation(false);
	}
	
	
	public List<Rule> getRules() {
		return rules;
	}
	
	public List<Variable> getVariables() {
		return variables;
	}

	public void setMethods(Map<String, InabitMethod> userMethods) {
		this.methods = userMethods;
	}


	@Override
	protected void initialize() {
		rules.clear();
	}


	@Override
	public void initAction(InabitSemanticAction action) {
		action.setMethods(methods);
		action.setVariablesList(variables);
		action.setRules(rules);
	}


	@Override
	public void deinitAction(InabitSemanticAction action) {
		rules = action.getDeclaredRules();
		variables = action.getVariablesList();
	}
}
